package com.fdq.dianping.forTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Random;

public class UserGenerator {
    private static final String URL = "jdbc:mysql://47.97.36.90:3306/dianping";
    private static final String USER = "root";
    private static final String PASSWORD = "873973409";

    public static void main(String[] args) {
        try {
            // 连接数据库
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            String sql = "INSERT INTO dp_user (phone, password) VALUES (?, ?)";

            PreparedStatement pstmt = conn.prepareStatement(sql);
            Random random = new Random();

            for (int i = 0; i < 1000; i++) {
                String phone = generatePhoneNumber(random);
                pstmt.setString(1, phone);
                pstmt.setString(2, "123456");
                pstmt.addBatch();

                // 每100条执行一次批处理
                if (i % 100 == 0) {
                    pstmt.executeBatch();
                }
            }

            pstmt.executeBatch(); // 插入剩余的
            pstmt.close();
            conn.close();
            System.out.println("成功插入1000个用户！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 生成随机手机号（以13/15/17/18/19开头）
    private static String generatePhoneNumber(Random random) {
        String[] prefixes = {"130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
                "150", "151", "152", "153", "155", "156", "157", "158", "159",
                "170", "171", "173", "175", "176", "177", "178",
                "180", "181", "182", "183", "185", "186", "187", "188", "189",
                "198", "199"};

        String prefix = prefixes[random.nextInt(prefixes.length)];
        StringBuilder phone = new StringBuilder(prefix);

        for (int i = 0; i < 8; i++) {
            phone.append(random.nextInt(10));
        }

        return phone.toString();
    }
}
